package com.boco.fznq.view;

import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.boco.fznq.model.BannerModel;
import com.culiu.core.utils.debug.DebugLog;
import com.culiu.core.imageloader.CuliuImageLoader;
import com.culiu.core.widget.CustomImageView;

/**
 * @describe Banner的ViewPager的适配器.
 * @author wangheng
 * @date: 2014年10月11日 下午2:17:12 <br/>
 */
public class BannerPagerAdapter extends PagerAdapter {

    /** 展示在ViewPager中的View的集合 **/
    private List<CustomImageView> viewList = null;

    private List<BannerModel> mBannerList = null;

    public BannerPagerAdapter(List<CustomImageView> list, List<BannerModel> bannerList) {
        viewList = list;
        this.mBannerList = bannerList;
    }

    @Override
    public int getCount() {
        if (viewList != null) {
            return viewList.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if(viewList != null && viewList.size() != 0) {
            int pageIndex = position/* % viewList.size()*/;
            try {
                /*if(viewList.get(pageIndex).getParent() != null){
                    ((ViewGroup)viewList.get(pageIndex).getParent()).removeView(viewList.get(pageIndex));
                }*/
                CustomImageView bannerView = viewList.get(pageIndex);
                container.addView(bannerView, 0);
                BannerModel banner = mBannerList.get(position);
                CuliuImageLoader.getInstance().display(bannerView, banner.getUrl());
                return bannerView;
            } catch(Exception e) {
                DebugLog.e(e.getMessage());
            }
        }
        return null;
    }
    
    @Override
    public void destroyItem(View container, int position, Object object) {
        if(viewList != null && viewList.size() > position) {
            ImageView view = viewList.get(position);
            ((ViewPager)container).removeView(view);
        }
    }
}
